import User from "../../models/user";
import { config } from "../../config";

const app = getApp();
const user = new User();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: config.baseImgUrl,
    flag: true,
    openid: "",
    sessionKey: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const token = wx.getStorageSync("token");
    //检测用户是否登录过了
    if (token) {
      wx.switchTab({
        url: "/pages/home/index",
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  pwLogin() {
    wx.redirectTo({
      url: "/owner-app/pages/pw-login/index",
    });
  },
  //登录
  userWxLogin(data) {
    user
      .userWxLogin(data)
      .then((res) => {
        if (res.code === 1000) {
          const { user, token } = res.messageBody;
          wx.setStorageSync("user", user);
          wx.setStorageSync("token", token);
          wx.switchTab({
            url: "/pages/home/index",
          });
          return;
        }
      })
      .catch((err) => {
        console.log(err);
      });
  },
  onShareAppMessage: function () {},
  bindUserInfo() {
    wx.getUserProfile({
      desc: "获取你的昵称、头像、地区及性别",
      // desc: "请授权",
      success: (res) => {
        getApp().globalData.userInfo = res.userInfo;
        this.setData({
          flag: false,
        });
        this.wxlogin();
      },
      fail: (res) => {
        //用户按了拒绝按钮
        wx.showModal({
          title: "警告",
          content: "您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!",
          showCancel: false,
          confirmText: "返回授权",
          success: function (res) {
            // 用户没有授权成功，不需要改变 isHide 的值
            if (res.confirm) {
              console.log("用户点击了“返回授权”");
            }
          },
        });
      },
    });
  },
  wxlogin() {
    let _this = this;
    wx.login({
      success(res) {
        if (res.code) {
          // 用户信息解密
          user.userDecrypt(res.code).then((res1) => {
            if (res1.code === 1000) {
              _this.setData({
                openid: res1.result.openid,
                sessionKey: res1.result.session_key,
              });

              let data = {
                openid: res1.result.openid,
                sessionKey: res1.result.session_key,
                avateor: getApp().globalData.userInfo.avatarUrl,
                userName: getApp().globalData.userInfo.nickName,
              };
              _this.userWxLogin(data);
            }
          });
        }
      },
    });
  },
  getPhoneNumber: function (e) {
    let _this = this;
    let encryptedData = e.detail.encryptedData;
    let iv = e.detail.iv;

    if (e.detail.errMsg == "getPhoneNumber:ok") {
      let data = {
        openid: _this.data.openid,
        encryptedData,
        sessionKey: _this.data.sessionKey,
        iv,
        avateor: getApp().globalData.userInfo.avatarUrl,
        userName: getApp().globalData.userInfo.nickName,
      };
      _this.userWxLogin(data);
    } else {
      //   //用户按了拒绝按钮
      wx.showModal({
        title: "警告",
        content: "您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!",
        showCancel: false,
        confirmText: "返回授权",
        success: function (e) {
          // 用户没有授权成功，不需要改变 isHide 的值
          if (e.confirm) {
            console.log("用户点击了“返回授权”");
          }
        },
      });
    }
  },
});
